package com.vvlive.caibao.smartcity.ui;

import android.content.SharedPreferences;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.maps2d.AMapUtils;
import com.amap.api.maps2d.model.LatLng;
import com.vvlive.caibao.smartcity.base.BaseActivity;
import com.vvlive.caibao.smartcity.bean.MyLocationBean;
import com.vvlive.caibao.smartcity.utils.MyLocationUtil;
import com.vvlive.caibao.smartcity.view.CommonActionBar;
import com.vvlive.caibao.smartcity.view.CustomDialog;
import com.vvlive.caibao.smartcity.view.CustomDialog.OnDialogClickListener;

import org.kymjs.kjframe.ui.BindView;

import java.util.Calendar;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/7/2.
 * 考勤打卡页面
 */
public class CheckOnActivity extends BaseActivity implements MyLocationUtil.GetMyLocation {

    @BindView(id = R.id.check_on_about, click = true)
    private TextView mAbout;
    @BindView(id = R.id.check_on_button_checkon, click = true)
    private Button mCheckOn;
    @BindView(id = R.id.check_on_button_checkout, click = true)
    private Button mCheckOut;

    @BindView(id = R.id.layout_check_on)
    private RelativeLayout mLayout;

    private String mAddress = "";

    private Calendar mAccessTime;
    private int mDayOfHour;
    private int mMin;
    private SharedPreferences mSharedPreferences;
    private SharedPreferences.Editor mEditor;

    // 是否已经签到
    private boolean mHasCheckOn = false;

    @Override
    protected int getContentViewRes() {
        return R.layout.activity_check_on;
    }

    @Override
    public void initWidget() {
        CommonActionBar actionBar = getCommonActionBar();
        actionBar.setTitle("会议签到");
        actionBar.setMoreVisible(false);
        actionBar.setBackgroundAlpha(0);
        setCommonActionBarFloating(true);
        mAccessTime = Calendar.getInstance();
        new MyLocationUtil(this, this);
    }

    @Override
    public void widgetClick(View v) {
        super.widgetClick(v);

        switch (v.getId()) {
            case R.id.check_on_button_checkon:
                // 考勤
                if (!mHasCheckOn) {
                    if ((mDayOfHour = mAccessTime.get(Calendar.HOUR_OF_DAY)) == 8 &&
                                (mMin = mAccessTime.get(Calendar.MINUTE)) == 00 && (mAddress == "" || mAddress.equals(""))) {
                        checkOnDialog();
                    } else {
                        alert("亲，迟到喽" + "--->地址：" + mAddress);
                        checkOnDialog();
                    }

                }
                break;
            case R.id.check_on_button_checkout:
                // 签退
                if (mHasCheckOn) {
                    checkOutDialog();
                } else {
                    alert("还没有签到，无法执行此操作");
                }
                break;
            case R.id.check_on_about:
                // 关于加班的说明
                alert("签到说明。");
                break;
        }
    }

    private void checkOnDialog() {

        showCheckDialog("您现在的位置:" + mAddress, "现在确认签到？",
                new OnDialogClickListener() {
                    @Override
                    public void onPositive() {
                        toast("签到成功");
                        mCheckOn.setText("已签到");
                        mCheckOn.setBackgroundResource(R.drawable.selector_check_on_button_cancel);
                        mCheckOn.setClickable(false);
                        mHasCheckOn = true;
                    }

                    @Override
                    public void onNegative() {
                        toast("取消签到");
                    }
                });
    }

    private void checkOutDialog() {
        showCheckDialog("您现在的位置:" + mAddress, "现在确认签退？",
                new OnDialogClickListener() {
                    @Override
                    public void onPositive() {
                        toast("签退成功");
                        mCheckOut.setText("已签退");
                        mCheckOut.setBackgroundResource(R.drawable.selector_check_on_button_cancel);
                        mCheckOut.setClickable(false);
                    }

                    @Override
                    public void onNegative() {
                        toast("取消签退");
                    }
                });
    }

    private CustomDialog showCheckDialog(String title, String content, OnDialogClickListener listener) {
        CustomDialog dialog = new CustomDialog(this);
        dialog.setmDialogTitle(title);
        dialog.setmDialogContent(content);
        dialog.setOnDialogClickListener(listener);
        dialog.show();
        return dialog;
    }

    @Override
    public void MyLocation(MyLocationBean myLocationBean) {
        mAddress = myLocationBean.getFormatAddress();
        myLocationBean.getLatitude();
        myLocationBean.getLongitude();
        Log.e("zhihui", "纬度" + myLocationBean.getLatitude() + "经度" + myLocationBean.getLongitude() + "地址:" + mAddress);
        aaa();
    }

    public int aaa() {
        //先维度（Latiude）后经度（Longitude）
        LatLng start = new LatLng(1, 1);
        LatLng end = new LatLng(11, 1);

        int cc = (int) AMapUtils.calculateLineDistance(start, end);
        return cc;
    }
}
